package model

import (
	"gorm.io/gorm"
	"time"
	"unlimited-comments/global"
)

type SubComment struct {
	Id        uint32         `gorm:"column:id;type:int UNSIGNED;comment:子评论id;primaryKey;not null;" form:"id" json:"id"`                                          // 子评论id
	UserId    int32          `gorm:"column:user_id;type:int;comment:用户id;not null;" form:"user_id" json:"user_id"`                                                 // 用户id
	VideoId   int32          `gorm:"column:video_id;type:int;comment:视频id;not null;" form:"video_id" json:"video_id"`                                              // 视频id
	ParentId  int32          `gorm:"column:parent_id;type:int;comment:上级评论id;not null;" form:"parent_id" json:"parent_id"`                                       // 上级评论id
	RootId    int32          `gorm:"column:root_id;type:int;comment:根评论id;not null;" form:"root_id" json:"root_id"`                                               // 根评论id
	Content   string         `gorm:"column:content;type:varchar(300);comment:评论内容;not null;" form:"content" json:"content"`                                      // 评论内容
	CreatedAt time.Time      `gorm:"column:created_at;type:datetime(3);comment:发布时间;not null;default:CURRENT_TIMESTAMP(3);" form:"created_at" json:"created_at"` // 发布时间
	UpdatedAt time.Time      `gorm:"column:updated_at;type:datetime(3);comment:修改时间;not null;default:CURRENT_TIMESTAMP(3);" form:"updated_at" json:"updated_at"` // 修改时间
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);comment:删除时间;default:NULL;" form:"deleted_at" json:"deleted_at"`                          // 删除时间
}

func (c *SubComment) TableName() string {
	return "sub_comment"
}

func (c *SubComment) AddComment() error {
	return global.DB.Create(&c).Error
}

func (c *SubComment) ListComment() (comment []*SubComment, err error) {
	err = global.DB.Where("root_id = ?", c.RootId).Find(&c).Error
	return
}

func (c *SubComment) DeleteSubComment() error {
	return global.DB.Delete(&c).Error
}
